package com.yjd.api.home;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.open.model.ApiListModel;
import com.yjd.comm.open.service.IApiListService;
import com.yjd.comm.util.FrameUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * <b>description</b>：api接口 <br>
 * <b>time</b>：2015-1-27下午7:55:27 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Controller
@Scope("prototype")
@RequestMapping("/apilist")
public class ApiListControl extends BaseControl {

	@Resource
	private IApiListService apiListService;

	/**
	 * 功能清单
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("")
	public String apilist() throws Exception {
		String version = FrameUtil.getConstResource("mfq.version");
		if("test".equals(version)){
			this.request.setAttribute("list", this.apiListService
					.getModelList(FrameUtil.newHashMap("is_delete", 0),DbWREnums.READ));
			return "open/apilist/index";
		}
		return "open/index";
	}

	/**
	 * 新增
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toadd")
	public String toadd() throws Exception {
		return getResult("edit");
	}

	/**
	 * 修改
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit/{id}")
	public String toedit(@PathVariable("id") Long id) throws Exception {
		if (id == null) {
			FrameUtil.throwBaseException1("参数有误!");
		}
		this.request
				.setAttribute("model", this.apiListService.getModelById(id, DbWREnums.READ));
		return getResult("edit");
	}

	/**
	 * 保存
	 * 
	 * @param al
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(ApiListModel al) throws Exception {
		if (al.getId() != null) {
			this.apiListService.update(al);
		} else {
			this.apiListService.insert(al);
		}
		return "redirect:/apilist";
	}

	/**
	 * 保存
	 * 
	 * @param al
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/del/{id}")
	public String del(@PathVariable("id") Long id) throws Exception {
		if (id == null) {
			FrameUtil.throwBaseException1("参数有误!");
		}
		ApiListModel am = this.apiListService.getModelById(id, DbWREnums.READ);
		if (am != null) {
			am.setIs_delete(1);
			this.apiListService.update(am);
		}
		return "redirect:/apilist";
	}

	public String getResult(String s) {
		return "open/apilist/" + s;
	}

}
